#ls calc class
import decimal
import math
import myglobal
import logging
import openpyxl
# 四舍六入五成双
import temp_cali
from xiuyue import Xy
def cha_value(cali,k):
    for i in range(len(cali)):
        if i==0:
            continue
        (k2,v2)=cali[i]
        if k<k2:
            (k1,v1)=cali[i-1]
            return (k-k1)/(k2-k1)*(v2-v1)+v1
    return None
class ClassLS():
    def __init__(self):
        super().__init__()
        self.type=None#试样类型
        self.gg=None#试样规格
        self.t=None#试验温度
        self.temp1=None
        self.temp2=None
        self.temp3=None
        self.d0=None#直径
        self.a0=None#厚度
        self.b0=None#宽度
        self.D0=None#外径
        self.S0=None#横截面积
        self.k=None#比例系数
        self.L0=None#标距
        self.Lu=None#断后标距
        self.du1=None#断后直径
        self.du2=None#断后直径
        self.Fm=None#
        self.Rm=None
        self.FeH=None
        self.ReH=None
        self.FeL=None
        self.ReL=None
        self.A=None
        self.Z=None 
        self.At=None 
        self.Ae=None 
        self.Ag=None 
        self.Agt=None 
        self.qqb=None 
        self.E=None 
        self.Fp02=None
        self.Rp02=None
    def calc(self):
        self.S0=self.jiemian()
        logging.info(self.S0)
    def jiemian(self):
        # =IF($D7="圆管",PI()*(J7^2-(J7-2*H7)^2)/4,
        # IF(OR($D7="棒材",$D7="圆棒"),PI()*G7^2/4,
        # IF($D7="板材",H7*I7,
        # IF($D7="扁钢丝",(H7*I7-0.18*H7*H7),
        # I7/4*(J7^2-I7^2)^0.5+J7^2/4*ASIN(I7/J7)-I7/4*((J7-2*H7)^2-I7^2)^0.5-((J7-2*H7)/2)^2*ASIN(I7/(J7-2*H7))))))
        try:
            if self.type=="圆管":#Sample_type.圆管:
                return math.pi*(self.D0*self.D0-self.d0*self.d0)/4
            elif self.type in ["棒材","圆棒"]:#Sample_type.棒材:#(OR($D7="棒材",$D7="圆棒"):
                return math.pi*self.d0*self.d0/4
            elif self.type=="板材":
                return self.a0*self.b0
            elif self.type=="扁钢丝":
                return (self.a0*self.b0-0.18*self.a0*self.a0)
            else:
                return self.b0/4*math.sqrt(self.D0*self.D0-self.b0*self.b0)+self.D0*self.D0/4*math.asin(self.b0/self.D0)-self.b0/4*math.sqrt(math.pow(self.D0-2*self.a0,2)-self.b0*self.b0)-math.pow((self.D0-2*self.a0)/2,2)*math.asin(self.b0/(self.D0-2*self.a0))
        except:
            return None
    def test(self):
        self.S0=self.jiemian() #横截面积  
        print(self.S0)
        self.k=5.65 #比例系数    
        self.L0=mround(self.k*math.sqrt(self.S0),5)
        #断后尺寸 标距 直径
        self.Lu=None
        self.du1=None
        self.du2=None
        self.Fm=2250.13012
        self.Rm="%.2f" % (self.Fm/self.S0*1000)
        print("Fm_",float('%.3g' % self.Fm))#有效位数
        print("Fm",self.Fm)
        print("Rm",self.Rm)
        # 四舍六入五成双
        # print(decimal.Decimal("1.15").quantize(decimal.Decimal("0.0"),rounding=decimal.ROUND_HALF_EVEN))
        # print(round_half_even("1.15",1))#1位小数
        # =IF(Y7="","",IF(Z$6="-",xy(MROUND(X7/$K7*1000,1),1),xy(MROUND(X7/$K7*1000,Z$6),Z$6)))
        xy_jg=1
        self.Rm_=xy(mround(float(self.Rm),xy_jg),xy_jg)
        print("Rm_",self.Rm_)
    def calc_t(self):
        self.temp1=cha_value(temp_cali.cali1,self.t)
        self.temp2=cha_value(temp_cali.cali2,self.t)
        self.temp3=cha_value(temp_cali.cali3,self.t)
        pass
def test3():
    s=ClassLS()
    s.t=950
    s.calc_t()
    print(s.t,s.temp1,s.temp2,s.temp3)
def test1():
    s=ClassLS()
    s.type="圆管" #Round_tube
    s.gg="2.0" #试样规格 mm
    s.t=25  #试验温度 ℃
    #原始尺寸
    s.a0=2   #厚度
    s.b0=100   #宽度
    s.D0=30   #外径 
    s.d0=s.D0-2*s.a0   #直径 #内径
    s.test()
def test2():
    x=Xy()
    # print(x.xy(math.pi))
    print(x.xiuyue("3.1415"))
    print(x.xiuyue("3.1425"))
    x=Xy()
    print(x.xiuyue("3.1415"))
    print(x.xiuyue("3.1425"))
    print(round_half_even("3.1415",3))
    print(round_half_even("3.1425",3))
if __name__=="__main__":
    # test1()
    # test2()
    test3()